/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.model;

import java.lang.reflect.InvocationTargetException;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.resources.RemoteFolder;
import net.sourceforge.perforce.ui.PerforceUIPlugin;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class RemoteFolderElement extends PerforceModelElement implements IWorkbenchAdapter {
  // ---------------------------------------------------------- Public Methods
  // (IWorkbenchAdapter)

  /** @see IWorkbenchAdapter#getChildren(Object) */
  public Object[] getChildren(Object object) {
    if (!(object instanceof RemoteFolder)) {
      return new Object[0];
    }

    final RemoteFolder folder = (RemoteFolder) object;
    final Object[][] result = new Object[1][];

    Shell shell = new Shell(Display.getDefault());
    try {
      run(true, false, new IRunnableWithProgress() {
        /** @see IRunnableWithProgress#run(IProgressMonitor) */
        public void run(IProgressMonitor monitor) throws InvocationTargetException {
          try {
            result[0] = folder.members(monitor);
          } catch (PerforceException e) {
            throw new InvocationTargetException(e);
          }
        }
      });
    } catch (InvocationTargetException e) {
      handle(e);
    } catch (InterruptedException e) {
      handle(e);
    } finally {
      shell.dispose();
    }

    return result[0];
  }

  /** @see IWorkbenchAdapter#getImageDescriptor(Object) */
  public ImageDescriptor getImageDescriptor(Object object) {
    if (!(object instanceof RemoteFolder)) {
      return null;
    }

    RemoteFolder remoteFolder = (RemoteFolder) object;
    if (remoteFolder.getParent() != null) {
      return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
          ISharedImages.IMG_OBJ_FOLDER);
    } else {
      return PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.IMG_ROOTELEMENT);
    }
  }

  /** @see IWorkbenchAdapter#getLabel(Object) */
  public String getLabel(Object object) {
    if (!(object instanceof RemoteFolder)) {
      return null;
    }

    RemoteFolder folder = (RemoteFolder) object;
    return folder.getName();
  }

  /** @see IWorkbenchAdapter#getParent(Object) */
  public Object getParent(Object o) {
    return null;
  }
}
